package mainFrame;

import context.*;

import javax.swing.JFrame;
import javax.swing.JTabbedPane;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
import javax.swing.JFileChooser;

import util.EditorProperties;
import util.FillStyle;
import util.FontStyle;
import util.StrokeStyle;

import java.awt.Component;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Toolkit;

import java.io.File;
import java.net.URL;

// ***************************************************************************
// *** CLASS: MainFrame
// ***************************************************************************
public class MainFrame implements Context.Constants
{ //private static final boolean DEBUG=true; // Overriding Context.Constants.DEBUG
  static final String TAG="mainFrame.MainFrame";

  protected static JFrame frame;
  public static JFrame getFrame() { return(frame); }
  protected static FontStyle currentFont=new FontStyle();
  public static FontStyle getCurrentFont() { return(currentFont); }
  public static void setCurrentFont(FontStyle font) { currentFont=font; }

  protected static StrokeStyle currentStroke=new StrokeStyle();
  public static StrokeStyle getCurrentStroke() { return(currentStroke); }
  public static void setCurrentStroke(StrokeStyle stroke) { currentStroke=stroke; }
  
  protected static FillStyle currentFill=new FillStyle();
  public static FillStyle getCurrentFill() { return(currentFill); }
  public static void setCurrentFill(FillStyle fill) { currentFill=fill; }
  
  protected static TriPane triPane;

  protected static StatusLine statusLine;
  public static StatusLine getStatusLine() { return(statusLine); }
  protected static ControlPanel controlPanel;
  public static ControlPanel getControlPanel() { return(controlPanel); }

  protected JTabbedPane tabbedMap;
  protected static DrawPanel currentDrawPanel;
  public static DrawPanel getCurrentDrawPanel() { return(currentDrawPanel); }
  
  protected static JTabbedPane tabbedTable;
  protected static TablePane selectedTable;
  public static TablePane getSelectedTable() { return(selectedTable); }
  
  public static void repaint()
  { if(selectedTable!=null) selectedTable.updateTable();
	triPane.repaint();
  }

  //***************************************************************************
  //*** LOCAL CLASS: TriPane
  //***************************************************************************
  protected class TriPane extends JPanel
  { static final long serialVersionUID=0; // AD'HOC
    public TriPane(Component leftComponent,Component rightComponent,Component bottomComponent)
    { super();
      Dimension screenSize=Toolkit.getDefaultToolkit().getScreenSize();
      setDoubleBuffered(true);
      setLayout(new BorderLayout());
      JSplitPane topPane=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,leftComponent,rightComponent);
//      topPane.setDividerLocation(130);
      topPane.setDividerLocation((int)(screenSize.getWidth()*0.10)); // 10 %
      JSplitPane splitPane=new JSplitPane(JSplitPane.VERTICAL_SPLIT,topPane,bottomComponent);
      splitPane.setPreferredSize(new Dimension(900,700));
      splitPane.setDividerLocation(500);
//      splitPane.setDividerLocation((int)(screenSize.getHeight()*0.80)); // 80 %
      add(splitPane,BorderLayout.CENTER);
    }
  }

  
  public static URL selectImageFileDialog()
  { try
    { if(DEBUG) Log.d(TAG,"selectFileDialog");
      final JFileChooser fc=new JFileChooser();
      File dir=new File(EditorProperties.getWorkSpaceDirectory()+"/image");
      dir.mkdirs();
      fc.setDialogTitle("Select Image File");
      fc.setCurrentDirectory(dir);
      fc.setSelectedFile(new File("Default.png"));
      int returnVal = fc.showOpenDialog(frame);
      if(returnVal!=JFileChooser.APPROVE_OPTION)
      { if(DEBUG) Log.d(TAG,"selectFileDialog: cancelled by user.");
        return(null);
      }
      File selectedFile=fc.getSelectedFile();
      if(DEBUG) Log.d(TAG,"newDialog: Selected File: "+selectedFile.getAbsolutePath()+", Name: "+selectedFile.getName()
    		  +", exists="+selectedFile.exists()+", canRead="+selectedFile.canRead());
      if(selectedFile.canRead()) return(selectedFile.toURI().toURL());
      return(null);
    } catch (Exception e) { Log.e(TAG,"newDialog: ",e); }
    return(null);
  }

}
